package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.os.RemoteException;
import android.widget.Toast;
import com.sunmi.electronicscaleservice.ScaleCallback;
import com.sunmi.scalelibrary.ScaleManager;
import com.zbw.weightlibrary.Connector.BaseControlImpl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.R;

import java.text.DecimalFormat;

/**
 * 作者 ：Wesley
 * 时间 ：2019-02-18 17:31
 * 这个类是干嘛的？：SunmiUtil
 */
public class SunmiUtil extends BaseControlImpl {
    private final Context mContext;
    private final IinfoCallback mCallback;
    private ScaleManager scaleManager;
    private boolean isScaleSuccess;
    protected boolean ReadWeight;//是否读取重量
    public static int net;
    public int pnet;//皮重
    //状态
    public int status;
    public SunmiUtil(Context context, IinfoCallback callback) {
        super(callback);
        mContext = context;
        mCallback = callback;
    }

    private void sunmi_connect(){
        scaleManager = ScaleManager.getInstance(mContext);
        //绑定sunmi service
        scaleManager.connectService(new ScaleManager.ScaleServiceConnection() {
            @Override
            public void onServiceConnected() {
                //服务绑定
                mCallback.onConnect("绑定成功！",true);
            }
            @Override
            public void onServiceDisconnect(){
                //服务解绑
                isScaleSuccess = false;
            }
        });
    }


    @Override
    public void bleConnect() {
        sunmi_connect();
    }

    @Override
    public void bleDisconnect() {
        try {
            scaleManager.cancelGetData();
            scaleManager.onDestroy();
            scaleManager = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void getWeight() {
        ReadWeight = true;
        try {
            scaleManager.getData(new ScaleCallback.Stub(){
                @Override
                public void getData(final int Net, int Tare, final int Status) throws RemoteException {
//                    获取净重，单位：克 Net 净重，单位：克
//                    获取⽪皮重量，单位：克 Tare ⽪重，单位：克
//                    稳定标志 Status 0:动态；1:稳定
                    net = Net;
                    pnet = Tare;
                    if (mCallback != null&&scaleManager != null&&ReadWeight) {
                        isScaleSuccess = true;
                        mCallback.onGetWeight(decimalFormat.format(Net * 1.0f / 1000), (Status & 1) == 1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void stopWeight() {
        ReadWeight = false;
    }

    /**
     * 清皮
     */
    @Override
    public void clearTare() {
        if (pnet + net == 0 && isScaleSuccess) {
            tare();
        } else {
            Toast.makeText(mContext, R.string.scale_tips_clearfail, Toast.LENGTH_LONG).show();
        }
    }
    /**
     * 去皮
     */
    @Override
    public void tare() {
        try {
            if (isScaleSuccess) {
                scaleManager.tare();
                Toast.makeText(mContext, getPnet() == 0 ? R.string.more_clear_peele : R.string.more_peele, Toast.LENGTH_LONG).show();
            }
        } catch (RemoteException e) {
            Toast.makeText(mContext, getPnet() == 0 ? R.string.more_clear_peele_fail+e.getMessage() : R.string.more_peele_fail+e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    /**
     * 清零
     */
    @Override
    public void zero() {
        try {
            if (isScaleSuccess) {
                scaleManager.zero();
                Toast.makeText(mContext, R.string.more_clear, Toast.LENGTH_LONG).show();
            }
        } catch (RemoteException e) {
            Toast.makeText(mContext, R.string.more_clear_fail+e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }


    public int getPnet() {
        return pnet;
    }

    public void setPnet(int pnet) {
        this.pnet = pnet;
    }

}
